<?php
/**
 * RSS component
 * Can parse RSS feeds from other sites and
 * create a feed.
 *
 * Uses MagpieRSS for reading
 * Uses FeedCreator for feeding
 *
 * @author      RosSoft
 * @version     0.1
 * @license	MIT
 *
 */

class RssComponent extends Object {
	var $controller;

	/**
	 * @var string $feed_format Format the feed should comply to.
	 * Valid values are:	"mbox", "RSS0.91", "RSS1.0", "RSS2.0", "OPML", "ATOM0.3".
	 */
	var $feed_format = 'RSS2.0';

	function startup(& $controller) {
		$this->controller = & $controller;
	}

	/**
	 * Fetch a RSS feed
	 * @param string $url The url of the RSS.
	 * @return object RSS Object. Example of usage:
	 *
	 * echo "<a href=".$rss->channel['link']."><B>".$rss->channel['title']."</B></a>";
	 	foreach ($rss->items as $item)
	 	{
	 		$href = $item['link'];
	 		$title = $item['title'];
	 		$desc = $item['description'];
			echo "<P><a href=$href>$title</a><BR>";
			if($desc)
				echo $desc;
		}
	*/
	function fetch($url) {
		if (!defined('MAGPIE_OUTPUT_ENCODING')) {
			define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
		}
		vendor('rss\rss_fetch');
		return fetch_rss($url);
	}

	/**
	 * Feed a RSS
		 * @param array  $data The array with the info.
		 * 	Every record has: title (needed), link (needed), description
		 * 	date(yy-mm-dd hh:mn:ss), source, author
	 * @param string $title Title of the Feed
	 * @param string $description Description of the Feed
	 * @param string $link ??
	 * @param string $syncation_url ??
	 *
	 */

	function feed($data, $title, $description = '', $link = '', $syndication_url = '') {
		vendor('feedcreator.class');

		$this->controller->autoRender = false;

		$rss = new UniversalFeedCreator();
		$rss->title = $title;
		$rss->description = $description;
		$rss->link = $link;
		$rss->syndicationURL = $syndication_url;
		// Initialize the cssStyleSheet var, needed to avoid an error with FeedCreator version 1.7.2-mod (latest version at time of writing)
		$rss->cssStyleSheet = "";

		foreach ($data as $post) {
			$item = new FeedItem();
			$item->title = $post['title'];
			/**
			 * Added by Tarique Sani <tarique@sanioft.com>
			 */
			$item->category = $post['category'];
			$item->link = isset ($post['link']) ? $post['link'] : '';
			$item->description = isset ($post['description']) ? $post['description'] : '';
			if (isset ($post['date'])) {
				list ($date, $hours) = split(' ', $post['date']);
				list ($year, $month, $day) = split('-', $date);
				list ($hour, $min, $sec) = split(':', $hours);
				$date = date("r", mktime($hour, $min, $sec, $month, $day, $year));
			} else {
				$date = date("r");
			}
			$item->date = $date;
			if (isset ($post['guid'])) {
				$item->guid = $post['guid'];
			} else {
				$item->guid = md5($post['title'] . $post['link'] . $post['description']);
			}
			$item->source = isset ($post['source']) ? $post['source'] : '';
			$item->author = isset ($post['author']) ? $post['author'] : '';
			$rss->addItem($item);
		}
		$rss->outputFeed($this->feed_format);
	}
}
?>
